﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics.Contracts;
using Streambolics.PtBurn;

namespace WindowsFormsApplication1
{
    public partial class MainForm : Form
    {
        private PtBurnJobServer _Queue;

        public MainForm()
        {
            InitializeComponent(); 
        }

        private void button1_Click (object sender, EventArgs e)
        {
                DoConnect (textBox1.Text);
                DoList ();
        }

        private void DoList ()
        {
            if (_Queue == null)
            {
                DoConnect (textBox1.Text);
            }
            try
            {
                listView_Jobs.Items.Clear ();
                foreach (PtBurnJob j in _Queue.Jobs)
                {
                    ListViewItem it = listView_Jobs.Items.Add (j.Name);
                    it.SubItems.Add (j.StatusText);
                }
                errorProvider.SetError (button_List, "");
            }
            catch (Exception ex)
            {
                errorProvider.SetError (button_List, ex.Message);
            }
        }

        private void DoConnect (string p)
        {
            try
            {
                if (p == null)
                {
                    _Queue = null;
                }
                else
                {
                    _Queue = new PtBurnJobServer (p);
                }
                errorProvider.SetError (button_Connect, "");
            }
            catch (Exception ex)
            {
                errorProvider.SetError (button_Connect, ex.Message);
            }

            button_Connect.Enabled = _Queue == null;
        }

        private void textBox1_TextChanged (object sender, EventArgs e)
        {
            DoConnect (null);
        }

        private void button_List_Click (object sender, EventArgs e)
        {
            DoList ();
        }

        private void timer_Tick (object sender, EventArgs e)
        {
            if (_Queue != null)
            {
                DoList ();
            }
        }
    }
}
